package com.mz.ieann.doplayer.audio.ui

import android.app.Dialog
import android.os.Bundle
import android.util.Log
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.View.OnClickListener
import android.view.ViewGroup
import android.widget.Button
import android.widget.LinearLayout
import android.widget.SeekBar
import android.widget.SeekBar.OnSeekBarChangeListener
import android.widget.TextView
import androidx.appcompat.widget.AppCompatSeekBar
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentTransaction
import com.mz.ieann.doplayer.R

/**
 * <p>------------------------------------------------------
 * <p>Copyright (C) 2020 wasu company, All rights reserved.
 * <p>------------------------------------------------------
 * <p>
 * <p>
 *
 * @author Created by zhangmz
 * @date on 2023/5/22
 */
class AudioControllerDialog:
    DialogFragment(R.layout.dialog_audio_controller),
        OnClickListener
{

    private lateinit var equalizerLl: LinearLayout
    private lateinit var dialogBassBoost: AppCompatSeekBar

    private var equalizerNum = 1

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setStyle(STYLE_NO_TITLE, R.style.DialogShowStyle)
    }

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        return super.onCreateDialog(savedInstanceState)
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        dialog?.window?.apply {
            attributes.gravity = Gravity.BOTTOM
            setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)
            setWindowAnimations(R.style.DialogShowAnimations)
        }
        return super.onCreateView(inflater, container, savedInstanceState)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        equalizerLl = view.findViewById(R.id.equalizerLl)
        val params  = ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)
        params.width = 1
        for (i in 0 until equalizerNum){
            val childView = LayoutInflater.from(context).inflate(R.layout.dialog_audio_child_equalizer, null)?: return
            childView.layoutParams = params
            val frequencySb = childView.findViewById<SeekBar>(R.id.frequencySb)
            val frequencyTv = childView.findViewById<TextView>(R.id.frequencyTv)
            frequencyTv.text = "${(i + 1) * 10}Hz"
            equalizerLl.addView(childView)
        }

        addListener(view)
    }

    private fun addListener(view: View){
        view.findViewById<Button>(R.id.dialogCancel).setOnClickListener(this)

        dialogBassBoost = view.findViewById(R.id.dialogBassBoost)
        dialogBassBoost.max = 1000
        dialogBassBoost.progress = 0
        dialogBassBoost.setOnSeekBarChangeListener(object :
            OnSeekBarChangeListener {
            override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) {
                Log.v("audio_player", "onProgressChanged, progress:$progress")
            }
            override fun onStartTrackingTouch(seekBar: SeekBar?) { }
            override fun onStopTrackingTouch(seekBar: SeekBar?) { }
        })
    }

    companion object Build {

        private var num = 1

        fun setEqualizerNum(num: Int): Build {
            this.num = num
            return this
        }

        fun show(manager: FragmentManager, tag: String? = null){
            val dialog = AudioControllerDialog()
            dialog.equalizerNum = num
            dialog.show(manager, tag)
        }

        fun show(manager: FragmentTransaction, tag: String? = null){
            val dialog = AudioControllerDialog()
            dialog.equalizerNum = num
            dialog.show(manager, tag)
        }

        fun showNow(manager: FragmentManager, tag: String? = null){
            val dialog = AudioControllerDialog()
            dialog.equalizerNum = num
            dialog.showNow(manager, tag)
        }
    }

    override fun onClick(v: View?) {
        when(v?.id){
            R.id.dialogCancel -> dismiss()
        }
    }

}